package com.mcintyret.site.service.journal;

import com.mcintyret.site.journal.Journal;
import com.mcintyret.site.journal.Timeseries;
import com.mcintyret.site.journal.database.JournalDao;
import com.mcintyret.site.journal.database.TimeseriesDao;
import com.mcintyret.site.web.controller.journal.forms.TimeseriesForm;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * User: mcintyret2
 * Date: 15/04/2012
 */

@Service
public class TimeseriesHelper {
    @Resource
    private JournalDao journalDao;
    @Resource
    private TimeseriesDao timeseriesDao;


    public void deleteTimeseriesWithId(int id) {
        Timeseries timeseries = timeseriesDao.get(id);
        timeseries.getJournal().getTextEntries().remove(timeseries);
    }

    public Integer addNewTimeseriesToJournal(TimeseriesForm form) {
        Timeseries newTimeseries = createTimeseriesFromForm(form);
        Journal journal = journalDao.get(form.getJournalId());
        journal.addTimeseries(newTimeseries);
        journalDao.flushSession();
        return newTimeseries.getId();
    }

    private Timeseries createTimeseriesFromForm(TimeseriesForm form) {
        return new Timeseries(form.getName(), form.getDescription());
    }

}
